/*
 * Copyright (C) 2019-2020 Alibaba Group Holding Limited
 */


#include <stdlib.h>
#include <string.h>
#include <aos/aos.h>
#include "aos/cli.h"
#include "app_main.h"
#include "app_init.h"
#include <devices/iic.h>
#include <devices/devicelist.h>
#include <drv/gpio.h>
#include "pin.h"

#define TAG "app"

#if (CONFIG_SPLIT_KWS_DNN > 0 && !CONFIG_DNN_NOT_USE_BIN)
#define ADDR_OF_MODEL_DATA_IN_SDRAM 0x18680000
const void *const g_kws_model_generic = (const void *)ADDR_OF_MODEL_DATA_IN_SDRAM;
#endif

void voice_app_init(void);

//TODO: for mca use
#if 0
#include "mca/mca.h"
#include "mca/mca_all.h"
static struct {
#define MCA_IIR_SAMPLES_MAX (512)
    fxp32_t                 coeff[5];
    fxp32_t                 input[MCA_IIR_SAMPLES_MAX];
    fxp32_t                 output[MCA_IIR_SAMPLES_MAX - 2];
} _mca_iir  __attribute__((section(".fast_heap")));
#endif

void main()
{
    board_yoc_init();
#if 1
#if 1
    extern int __chunk_buffer0_start;
    extern int __chunk_buffer0_end;
    extern int __chunk_buffer1_start;
    extern int __chunk_buffer1_end;
    extern int __fast_heap_start;
    extern int __fast_heap_end;
    extern int __data_end__;
//     TCM1_CHUNK_0   : ORIGIN = 0x80020000, LENGTH = 0x1000    /* CHUNK BUFFER 0 4KB */
//     TCM1_FAST_HEAP : ORIGIN = 0x80021000, LENGTH = 0xe000    /* FAST HEAP     56KB */
// 	  TCM1_CHUNK_1   : ORIGIN = 0x8002F000, LENGTH = 0x1000    /* CHUNK BUFFER 1 4KB */
    memset((void *)0x80020000, 0, 0x1000);
    memset((void *)0x8002F000, 0, 0x1000);
    memset((void *)0x80021000, 0, 0xe000);
    memcpy((void *)0x80020000, \
           (void *)(&__data_end__), \
           &__chunk_buffer0_end - &__chunk_buffer0_start);
    memcpy((void *)0x8002F000, \
           (void *)(&__data_end__ + (&__chunk_buffer0_end - &__chunk_buffer0_start)), \
           &__chunk_buffer1_end - &__chunk_buffer1_start);
    memcpy((void *)0x80021000, \
           (void *)(&__data_end__ + (&__chunk_buffer0_end - &__chunk_buffer0_start) + (&__chunk_buffer1_end - &__chunk_buffer1_start)), \
           &__fast_heap_end - &__fast_heap_start);
    csi_dcache_clean();
    csi_icache_invalid();
    LOGD(TAG, "%s\n", aos_get_app_version());

    voice_app_init();
#endif
#else

#if 0
    mcax_register_local();
    {
        int rc;
        fxp32_t coeff[5] = { 0xffae2458, 0x006c3bdc, 0xff5acd7a, 0x00d97320, 0xff09b684 };
        fxp32_t input[16] = { 0xda374d1e, 0x3fe6b88d, 0xa66f8f14, 0x7ba0c273, 0xeffffa6a, 0x477e84b9, 0xf9f5c520, 0xcefd255f,
                              0xfbcf8536, 0x745a2a65, 0xfb5d30ac, 0x79dd79cb, 0xffff1d82, 0x757f5991, 0x7adb01b8, 0xfb7e6fb7
                            };
        fxp32_t output[16 - 2];

        memcpy(_mca_iir.coeff, coeff, 16 * sizeof(fxp32_t));
        memcpy(_mca_iir.input, input, 16 * sizeof(fxp32_t));
        memset(_mca_iir.output, 0, sizeof(_mca_iir.output));

        mcax_t *mca = mcax_new(MCA_TYPE_IIR);
        rc = mcax_iir_fxp32_coeff32_config(mca, _mca_iir.coeff);
        rc = mcax_iir_fxp32(mca, _mca_iir.input, 16, 0, 0, _mca_iir.output);
        printf("====>>>mca = %p, rc = %d\n", mca, rc);
    }
#else
    mcaicore_cp_init();
#endif
#endif
}

